#include "classeditor.h"
#include "ui_classeditor.h"

#include "uml/class/umlclassitem.h"
#include "uml/projects/umlclassdiagramm.h"

ClassEditor::ClassEditor(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ClassEditor)
{
    ui->setupUi(this);

    this->_scene = new UmlClassDiagramm();
    this->_scene->setSceneRect(0, 0, 200, 50);
    this->ui->graphicsViewPreview->setScene(this->_scene);
    this->ui->graphicsViewPreview->setInteractive(false);

    this->_previewItem = new UmlClassItem();
    this->_previewItem->setProject(this->_scene);
    this->_previewItem->setShowAtts(false);
    this->_previewItem->setShowMeths(false);
    this->_scene->addItem(this->_previewItem);

    QWidget::setAttribute(Qt::WA_DeleteOnClose);

    // radiobuttons
    QObject::connect(this->ui->nameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(updatePreview()));
    QObject::connect(this->ui->checkBoxIsAbstract, SIGNAL(toggled(bool)), this, SLOT(updatePreview()));
    QObject::connect(this->ui->checkBoxIsInterface, SIGNAL(toggled(bool)), this, SLOT(updatePreview()));
    QObject::connect(this->ui->checkBoxIsAbstract, SIGNAL(toggled(bool)), this, SLOT(toggleIsAbstract()));
    QObject::connect(this->ui->checkBoxIsInterface, SIGNAL(toggled(bool)), this, SLOT(toggleIsInterface()));

    // save and cancel
    QObject::connect(this->ui->pushButtonSave, SIGNAL(clicked()), this, SLOT(save()));
    QObject::connect(this->ui->pushButtonCanel, SIGNAL(clicked()), this, SLOT(cancel()));

}

ClassEditor::~ClassEditor()
{
    this->_scene->clear();
    delete this->_scene;
    delete ui;
}

void ClassEditor::setClassItem(UmlClassItem *classItem)
{
    this->_classItem = classItem;
    this->ui->nameLineEdit->setText(this->_classItem->name());
    this->ui->checkBoxIsAbstract->setChecked(this->_classItem->isAbstract());
    this->ui->checkBoxIsInterface->setChecked(this->_classItem->isInterface());

    this->updatePreview();
}

void ClassEditor::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ClassEditor::save()
{
    this->_classItem->setText(this->ui->nameLineEdit->text());
    this->_classItem->setIsAbstract(this->ui->checkBoxIsAbstract->isChecked());
    this->_classItem->setIsInterface(this->ui->checkBoxIsInterface->isChecked());

    this->done(QDialog::Accepted);
}

void ClassEditor::cancel()
{
    this->done(QDialog::Rejected);
}

void ClassEditor::toggleIsAbstract()
{
    this->ui->checkBoxIsInterface->setChecked(false);
}

void ClassEditor::toggleIsInterface()
{
    this->ui->checkBoxIsAbstract->setChecked(false);
}

void ClassEditor::updatePreview()
{
    this->_previewItem->setText(this->ui->nameLineEdit->text());
    this->_previewItem->setIsAbstract(this->ui->checkBoxIsAbstract->isChecked());
    this->_previewItem->setIsInterface(this->ui->checkBoxIsInterface->isChecked());
    this->_previewItem->setPos(100 -this->_previewItem->boundingRect().width() / 2, 25 -this->_previewItem->boundingRect().height() / 2);
    this->_previewItem->update();

    if (this->_previewItem->isAbstract()) {
        qDebug() << "is abstract";
    }
}
